package net.lab1024.sa.base.module.support.job.sample;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.base.kafka.KafkaProducerService;
import net.lab1024.sa.base.module.support.config.ConfigDao;
import net.lab1024.sa.base.module.support.job.core.SmartJob;
import net.lab1024.sa.base.module.support.soaraction.service.SoarActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 定时任务 - ES测试数据生成
 */
@Slf4j
@Service
public class ClearSoarLog implements SmartJob {

    @Autowired
    private SoarActionService soarActionService;

    @Override
    public String run(String param) {
        JSONObject jsonObject = JSONObject.parseObject(param);
        long time = new Date().getTime();
        Integer days = jsonObject.getInteger("days");
        int i = days * 60 * 24 * 60 * 1000;
        long fiveMinutesAgo = time - i;
        String s = convertMillisToDateTime(fiveMinutesAgo);
        soarActionService.deleteByCreatime(s);
        return String.format("成功生成并写入 %d 条测试数据到ES索引: %s");
    }
    public static String convertMillisToDateTime(long millis) {
        // 1. 创建Instant对象（时间戳）
        Instant instant = Instant.ofEpochMilli(millis);

        // 2. 转换为带时区的日期时间（默认系统时区）
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
                .withZone(ZoneId.systemDefault());

        // 3. 格式化输出
        return formatter.format(instant);
    }

}
